package com.sxu.demo;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/hello")
    public String sayHello() {
        return "Hello World,Spring MVC";
    }

    @RequestMapping("/r1")
    public String r1(String name) {
        //接收url里的参数
        //只需要进行声明 spring自动就会接收
        return "接收到参数name："+name;
    }

    @RequestMapping("/r2")
    public String r2(String name,Integer age) {
        return "接收到参数name："+name+"年龄："+age;
    }

    @RequestMapping("/r3")
    public String r3(Student student) {
        return "接收到参数"+student;
    }

    @RequestMapping("/r4")
    public String r4(@RequestParam("name") String userName) {
        return "接收到参数name："+userName;
    }

    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "name",required = false) String userName) {
        return "接收到参数name："+userName;
    }

    @RequestMapping("/r6")
    public String r6(String[] array) {
        return "接收到参数array:"+ Arrays.toString(array);
    }

    @RequestMapping("/r7")
    public String r7(@RequestParam List<String> list) {
        return "接收到参数list:"+ list;
    }

    //使用 json传值
    @RequestMapping("/r8")
    public String r8(@RequestBody Student student) {
        return "接收到参数"+student;
    }

    //从路径中获取参数
    @RequestMapping("/article/{articleId}")
    public String r9(@PathVariable("articleId") Integer articleId) {
        return "接收到参数articleId:"+ articleId;
    }
    @RequestMapping("/article/{articleId}/{name}")
    public String r10(@PathVariable("articleId") Integer articleId,@PathVariable String name) {
        return "接收到参数articleId:"+ articleId + "name:"+ name;
    }

    //上传文件
    @RequestMapping("r11")
    public String r11(@RequestPart("file") MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        return "接收到文件 文件名："+originalFilename;
    }
}


